package Engine.UIControl
{
	import Interface.UIControl.ITextBase;
	
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.system.IME;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import library.Function.GlobalFun;

	public class TextBase implements ITextBase
	{
		private static var _isInput:Boolean = false;
		private static const STATE_STATIC:int = 0;
		private static const STATE_SELECT:int = 1;
		private static const STATE_INPUT:int = 2;
		
		private var _textFD:TextField;
		private var _isMouseIn:Boolean = false;
		private var _textState:int = 0;
		
		public static function get isInput():Boolean
		{
			return _isInput;
		}
		
		public function TextBase(txtTd:TextField = null,p_width:Number = 200.0,p_height:Number = 100.0)
		{
			if(txtTd == null)
			{
				this._textFD = new TextField();
				this._textFD.width = p_width;
				this._textFD.height = p_height;
				this._textFD.multiline = true;
				this._textFD.wordWrap = true;
				GlobalFun.setTextFormat(this._textFD);
			}
			else
			{
				this._textFD = txtTd;
			}
			this._textFD.selectable = false;
			this._textFD.mouseEnabled = false;
		}
		
		public function setMouseEnabled(enable:Boolean):void
		{
			this._textFD.mouseEnabled = enable;
			this._textFD.selectable = enable;
			if(enable)
			{
				this._textState = STATE_SELECT;
				this._textFD.addEventListener(MouseEvent.MOUSE_MOVE,onMouseIn);
				this._textFD.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
				this._textFD.addEventListener(FocusEvent.FOCUS_IN,onFouseIn);
				this._textFD.addEventListener(FocusEvent.FOCUS_OUT,onFouseOut);
			}
			else
			{
				this._textState = STATE_STATIC;
				this._textFD.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseIn);
				this._textFD.removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
				this._textFD.removeEventListener(FocusEvent.FOCUS_IN,onFouseIn);
				this._textFD.removeEventListener(FocusEvent.FOCUS_OUT,onFouseOut);
			}
		}
		
		public function get textField():TextField
		{
			return this._textFD;
		}
		
		public function setFocus(focus:Boolean):void
		{
			if(focus)
				this._textFD.stage.focus = this._textFD;
			else
				this._textFD.stage.focus = null;
		}
		
		public function setInputText(input:Boolean):void
		{
			if(input)
			{
				this.setMouseEnabled(true);
				this._textFD.type = TextFieldType.INPUT;
				this._textState = STATE_INPUT;
			}
			else
			{
				this.setMouseEnabled(false);
				this._textFD.type = TextFieldType.DYNAMIC;
			}
		}
		
		public function set text(txt:String):void
		{
			this._textFD.text = txt;
		}
		
		public function get text():String
		{
			return this._textFD.text;
		}
		
		private function onMouseIn(event:MouseEvent):void
		{
			if(this._isMouseIn)
				return;
			this._isMouseIn = true;
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			this._isMouseIn = false;
		}
		
		private function onFouseIn(event:FocusEvent):void
		{
			if(this._textState != STATE_INPUT)
			{
				IME.enabled = false;
			}
			else{
				TextBase._isInput = true;
				IME.enabled = true;
			}
			
		}
		
		private function onFouseOut(event:FocusEvent):void
		{
			TextBase._isInput = false;
			IME.enabled = false;
		}
	}
}